﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Workflows.Components.Entities.Relations.InferredAssociations
{
    [Serializable]
    public class BinaryTree : Tree
    {
        private bool _LeftChildIsBranch;
        /// <summary></summary>
        public bool LeftChildIsBranch
        {
            get
            {
                return this._LeftChildIsBranch;
            }
            set
            {
                this._LeftChildIsBranch = value;
            }
        }

        private bool _RightChildIsBranch;
        /// <summary></summary>
        public bool RightChildIsBranch
        {
            get
            {
                return this._RightChildIsBranch;
            }
            set
            {
                this._RightChildIsBranch = value;
            }
        }

        private object _LeftChild;
        /// <summary></summary>
        public object LeftChild
        {
            get
            {
                return this._LeftChild;
            }
            set
            {
                this._LeftChild = value;
            }
        }

        private object _RightChild;
        /// <summary></summary>
        public object RightChild
        {
            get
            {
                return this._RightChild;
            }
            set
            {
                this._RightChild = value;
            }
        }

        public BinaryTree(int id, string label)
            : base(id, label)
        { }

        public BinaryTree(int id, string label, BinaryTree parent, double distanceToParent)
            : base(id, label)
        {
            this.Parent = parent;

            if (parent != null && parent.Branches != null && !parent.Branches.ContainsKey(this))
            {
                int childrenCount = 0;
                if (parent.Branches != null)
                    childrenCount = parent.Branches.Count;
                if (parent.Leaves != null)
                    childrenCount += parent.Leaves.Count;
                if (childrenCount < 2)
                    parent.Branches.Add(this, distanceToParent);
            }
        }
    }
}
